<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function index(Request $request)
    {
        return view('index.home', [
            'name' => 'liqiangzhuang',
            'title' => '首页',
            'description' => '这是应用的首页。',
            'keywords' => '首页, 应用, 首页',
            'css' => [
                'css/home.css',
                'css/icon.css',
                'css/swiper.min.css',
            ],
            'js' => [
                'js/swiper.min.js',
                'js/home.js',
            ],
        ]);
    }

    public function hello(Request $request)
    {
        // 获取app实例
        $app = app();

        // 模拟从db 获取组件名
        $components = [
        'Juemiao\NavHeader\NavHeader', 
        'Juemiao\Swiper\Swiper', 
        'Juemiao\Child\Child',
        'Juemiao\Swiper\Swiper', 
        
        'Juemiao\Swiper\Swiper', 
        'Juemiao\TestParent\TestParent',
        'Juemiao\Child\Child',
        'Juemiao\Footer\Footer',
        ];
        $htmls = [];
        // 模拟从db 获取组件参数
        $params = [
            'Juemiao\Footer\Footer' => [
                'copyright' => 'Copyright © 2024 上海珏妙信息科技有限公司-1',
                'beianhao' => '沪ICP备20009428号-3-haha',
            ],
            'Juemiao\Test\Test' => [
                'list' => [
                    'row 001',
                    'row 002',
                    'row 003',
                ],
                'child' => [
                    'name' => 'David',
                ],
            ],
        ];
        $index = 0;
        array_map(function($component_name) use(&$htmls, $app, $params, &$index) {
            $component = $app->make($component_name);
            if (method_exists($component, 'setParams') && isset($params[$component_name])) {
                // die($component_name);
                $component->setParams($params[$component_name]);
            }
            if ($index == 3) {
                $component->setParams([
                    'slides' => [
                        [
                            'title' => 'Slide 1',
                            'image' => asset('image/website.jpg'),
                        ],
                        [
                            'title' => 'Slide 2',
                            'image' => asset('image/website.jpg'),
                        ],
                        [
                            'title' => 'Slide 3',
                            'image' => asset('image/website.jpg'),
                        ],
                    ],
                ]);
            }
            $view = $component->getView()->with('msg', '');
            $html = $view->render();
            if (strpos($component_name, 'Juemiao\Swiper\Swiper') !== false) {
                $html = '<div style="width: 100%; display: grid; grid-template-columns: 1fr 1fr;gap: 10px; height: 230px;"><div style="width: 100%; height: 100%;">' . $html . '</div></div>';
            } elseif (strpos($component_name, 'Juemiao\NavHeader\NavHeader') !== false) {
                $html = '<div style="width: 100%; display: grid; grid-template-columns: 1fr 1fr;gap: 10px; height: 230px;"><div style="width: 100%; height: 100%;">' . $html . '</div></div>';
            }
            $htmls[] = $html;
            $index ++;
        }, $components);
        $html =$this->extractJSCSS(implode('', $htmls));
        $htmls = [$html];
        // print_r($htmls);
        return view('app.index', [
            'my_components' => $htmls,
        ]);
    }
}
